package com.shuai.shuaisecondhand_springboot.controller;

import com.shuai.shuaisecondhand_springboot.common.Constants;
import com.shuai.shuaisecondhand_springboot.common.Result;
import com.shuai.shuaisecondhand_springboot.entity.User;
import com.shuai.shuaisecondhand_springboot.exception.ServiceException;
import com.shuai.shuaisecondhand_springboot.service.UserService;
import com.shuai.shuaisecondhand_springboot.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //登录
    @GetMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password) {
        if (userService.login(username, password) != null) {
            // 设置token
            String userId = userService.findId(username);//根据用户名去查找ID
            String token = TokenUtils.genToken(userId, password);
            User user = userService.login(username, password);
            user.setToken(token);

            return Result.success(user);
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");//自定义异常
        }

    }

    //注册
    // // user/registerUser?username=shuai&password=shuai
    @PostMapping("/registerUser")
    public Result register(@RequestParam String username, @RequestParam String password) {
        if (userService.registerUser(username, password)) {
            return Result.success("注册成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "该用户名已被注册");//自定义异常
        }
    }

    //修改个人密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestParam("id") String id, @RequestParam String password, @RequestParam String newpassword) {
        //为了将前端传来的string类型转化为数据库对应的int类型，做出如下操作
        Integer Iid = Integer.parseInt(id);

        if (userService.updatePassword(Iid, password, newpassword)) {
            return Result.success("修改密码成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "修改失败：密码错误");//自定义异常
        }
    }

    //查询
    @GetMapping("/getUser")
    public List<User> getUser() {
        return userService.findAll();
    }

    //根据id查询数据
    @GetMapping("/getOneUser")
    public User findOne(@RequestParam("id") String id) {
        Integer Iid = Integer.parseInt(id);
        return userService.findOne(Iid);
    }

    //增加
    @PostMapping("/insertUser")
    public Result adduser(@RequestBody User user) {
        if (userService.insertUser(user)) {
            return Result.success("添加成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名重复");//自定义异常
        }

    }

    //修改
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        log.warn("user:{}", user);
        if (userService.updateUser(user)) {
            return Result.success("修改成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名重复");//自定义异常
        }
    }

    //删除
    @DeleteMapping("/deleteUser/{id}")
    public Integer deleteUser(@PathVariable("id") Long id) {
        return userService.deleteUser(id);
    }

    //批量删除
    @PostMapping("/deleteUser/batch")
    public Integer deleteUserBatch(@RequestBody List<Integer> ids) {
        return userService.deleteUserBatch(ids);
    }

    //分页查询
    // 接口路径：/user/page?pageNum=1&pageSize=10
    // @RequestParam接受
    // limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/getUserPage")
    public Map<String, Object> getUserPage(@RequestParam Integer pageNum,
                                              @RequestParam Integer pageSize,
                                              @RequestParam String username) {
        pageNum = (pageNum - 1) * pageSize;
        List<User> data = userService.getUserPage(pageNum, pageSize, username);
        Integer total = userService.getTotal(username);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //查询总用户数
    @GetMapping("/getUserAllTotal")
    public int getAllTotal() {
        return userService.getAllTotal();
    }

}
